<?php

namespace app\index\controller;

use app\Func\RedisFunc;
use app\service\LDService;
use think\Config;
use Web3\Contract;

class Base
{
    protected $ldAddress;
    protected $ldAbi;
    protected $peer;
    protected $contract;

    protected $redis;

    protected $lDService;

    public function __construct()
    {
        $config = Config::load(APP_PATH . '../config/solidity.php');
        $this->ldAddress = $config['ld_address'];
        $this->ldAbi = $config['ld_abi'];
        $this->peer = $config['peer'];
        $obj = new Contract($this->peer, $this->ldAbi);
        $this->contract = $obj->at($this->ldAddress);

        $this->redis = new RedisFunc();

        $this->lDService = new LDService();
    }

    public function api_export($data) {
        $data_export = [];
        $data_export['error_code'] = 0;
        $data_export['error_msg'] = "";
        $data_export['data'] = $data;
        echo json_encode($data_export);
    }

    public function api_error($errorCode, $errorMsg) {
        $data = [
            'error_code' => $errorCode,
            'error_msg' => $errorMsg
        ];
        echo json_encode($data);
        exit();
    }

    public function get_curl($url){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $output = curl_exec($ch);

        curl_close($ch);
        return $output;
    }
}
